package com.mzw.huaxiarealestatebackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mzw.huaxiarealestatebackend.common.utils.DateFormat;
import com.mzw.huaxiarealestatebackend.entity.Client;
import com.mzw.huaxiarealestatebackend.entity.User;
import com.mzw.huaxiarealestatebackend.dao.UserDao;
import com.mzw.huaxiarealestatebackend.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * (User)表服务实现类
 *
 * @author makejava
 * @since 2023-12-22 17:34:43
 */
@Service("userService")
public class UserServiceImpl  implements UserService {
    @Resource
    private UserDao userDao;

    /**
     * 查询所有
     */
    public List<User> queryAll(){
        return this.userDao.selectList(null);
    }

    /**
     * 根据用户role查询用户
     * @param userRole
     * @return
     */
    @Override
    public User queryByRole(String userRole) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        // 根据用户角色进行查询
        lqw.eq(User::getUserRole, userRole);

        // 执行查询
        return this.userDao.selectOne(lqw);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param userId 主键
     * @return 实例对象
     */
    @Override
    public User queryById(String userId) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        User user = userDao.selectOne(queryWrapper);
        return user;
    }

    /**
     * 新增数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public boolean insert(User user) {
        return this.userDao.insert(user) > 0;
    }

    /**
     * 修改数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public boolean update(User user) {

        return this.userDao.update(user) > 0;
    }

    /**
     * 通过主键逻辑删除数据
     *
     * @param userId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String userId) {
        // 获取当前日期和时间
        LocalDateTime currentDateTime = LocalDateTime.now();
        // 将 LocalDateTime 转换为 Date
//        Date currentDate = Date.from(currentDateTime.atZone(ZoneId.systemDefault()).toInstant());
        User user = this.queryById(userId);
        user.setUserStatus("0");
        user.setUpdateTime(DateFormat.getNowDateFormat());
        boolean update = this.update(user);
        return update;
    }

    @Override
    public User getUserInfoById(String uid) {
        return userDao.selectUserInfoById(uid);
    }
    @Override
    public User getUserById(Integer uid) {
        return userDao.getUserById(uid);
    }
}
